import java.io.*;
import java.util.Scanner;
import java.util.Base64;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;

public class LoadIn extends JFrame implements ActionListener {
    public static void main(String[] args) {
        new LoadIn();
    }

    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField tf;
    private JPasswordField psf;
    private JRadioButton rb1;
    private JRadioButton rb2;
    private JButton bt1;
    private JButton bt2;

    public LoadIn() {
        this.setVisible(true);
        this.setSize(285, 190);
        this.setLocation(400, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        super.setTitle("德州扑克 - 登录");
        label1 = new JLabel("德州扑克登录界面");
        label2 = new JLabel("账号：");
        label3 = new JLabel("密码：");
        tf = new JTextField();
        psf = new JPasswordField();
        rb1 = new JRadioButton("记住账号");
        rb1.setSelected(true);
        rb2 = new JRadioButton("记住密码");
        bt1 = new JButton("登录");
        bt2 = new JButton("新用户注册");
        bt1.addActionListener(this);
        bt2.addActionListener(this);
        rb1.addActionListener(this);
        rb2.addActionListener(this);

        try (Scanner in = new Scanner(new File("userinfo.dat"))) {
            if (in.hasNextLine()) {
                tf.setText(in.nextLine());
            }
            if (in.hasNextLine()) {
                psf.setText(new String(Base64.getDecoder().decode(in.nextLine()), "utf-8"));
                rb2.setSelected(true);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        // 为指定的 Container 创建 GroupLayout
        GroupLayout layout = new GroupLayout(this.getContentPane());
        this.getContentPane().setLayout(layout);

        // 创建GroupLayout的水平连续组，，越先加入的ParallelGroup，优先级级别越高。
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        // 添加间隔
        hGroup.addGap(5);
        hGroup.addGroup(layout.createParallelGroup().addComponent(label2).addComponent(label3));
        hGroup.addGap(5);
        GroupLayout.SequentialGroup hGroup01 = layout.createSequentialGroup().addComponent(bt1).addGap(20)
                .addComponent(bt2);
        GroupLayout.SequentialGroup hGroup02 = layout.createSequentialGroup().addComponent(rb1).addGap(10)
                .addComponent(rb2);
        hGroup.addGroup(layout.createParallelGroup().addComponent(label1).addComponent(psf).addGroup(hGroup02)
                .addComponent(tf).addGroup(hGroup01));
        hGroup.addGap(5);
        // 设置水平分组
        layout.setHorizontalGroup(hGroup);

        // 创建GroupLayout的垂直连续组，，越先加入的ParallelGroup，优先级级别越高。
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        vGroup.addGap(10);
        vGroup.addGroup(layout.createParallelGroup().addComponent(label1));
        vGroup.addGap(10);
        vGroup.addGroup(layout.createParallelGroup().addComponent(label2).addComponent(tf));
        vGroup.addGap(5);
        vGroup.addGroup(layout.createParallelGroup().addComponent(label3).addComponent(psf));
        GroupLayout.ParallelGroup vGroup2 = layout.createParallelGroup().addComponent(rb1).addGap(20).addComponent(rb2);
        vGroup.addGroup(layout.createParallelGroup().addGroup(vGroup2));
        GroupLayout.ParallelGroup vGroup3 = layout.createParallelGroup().addComponent(bt1).addGap(10).addComponent(bt2);
        vGroup.addGroup(layout.createParallelGroup(Alignment.TRAILING).addGroup(vGroup3));
        vGroup.addGap(10);
        // 设置垂直组z
        layout.setVerticalGroup(vGroup);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == bt1) {
            if (tf.getText().isEmpty()) {// 账号不能为空
                return;
            }
            if (String.valueOf(psf.getPassword()).isEmpty()) {// 密码不能为空
                return;
            }
            // 登录服务器
            Test.setAccountName(tf.getText());
            Test.setAccountPassword(String.valueOf(psf.getPassword()));
            Test.link.println("<SignIn>");
            Test.link.println(Test.getAccountName());
            Test.link.println(Test.getAccountPassword());
            try {
                if (Test.link.readLine().equals("登录成功")) {
                    System.out.println("登录成功");
                } else {
                    System.err.println("登录失败");
                    return;
                }
            } catch (IOException ioe) {
                System.err.println("服务器错误");
                label1.setText("德州扑克登录界面 - 服务器错误");
                ioe.printStackTrace();
                return;
            }
            // 切换窗口
            this.setVisible(false);
            new Choice(tf.getText());
            // 保存账户和密码
            try (BufferedWriter out = new BufferedWriter(new FileWriter("userinfo.dat"))) {
                if (rb1.isSelected()) {
                    out.write(tf.getText());
                }
                if (rb2.isSelected()) {
                    out.write('\n');
                    out.write(Base64.getEncoder().encodeToString(String.valueOf(psf.getPassword()).getBytes("utf-8")));
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        } else if (e.getSource() == bt2) {
            this.setVisible(false);
            new Register(this);
        } else if (rb2.isSelected() && !rb1.isSelected()) {
            rb1.setSelected(true);
            // this.repaint();
        }
    }

    public void jumpFrom(JFrame jf) {
        jf.setVisible(false);
        this.setVisible(true);
    }
}
